import json

from datetime import datetime

import scrapy
from flask import current_app
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from spiders.items import StockFundamentalItem



class StockFundamentalSpider(scrapy.Spider):
    name = 'stock_fundamental'
    stock_list = []

    def start_requests(self):
        '''
        代替start_urls,从数据库读取板块编码，拼接读取股票信息
        :return: 示例：http://data.eastmoney.com/bkzj/BK0537.html
        http://quote.eastmoney.com/sz002240.html
        http://quote.eastmoney.com/concept/sh600031.html#
        '''
        # 深市  000001
        url_1 = 'https://push2.eastmoney.com/api/qt/stock/get?' \
                'ut=fa5fd1943c7b386f172d6893dbfba10b&' \
                'fltt=2&invt=2&' \
                'fields=f55,f84,f85,f92,f173,f105,f116,f117,f186,f187,f188,f190,f162,f167,f189&' \
                'secid=0.{0}&' \
                'cb=jQuery112409549122037220288_1641729910503&' \
                '_=1641729910531'
        # 沪市
        url_2 = 'http://push2.eastmoney.com/api/qt/stock/get?' \
                'ut=fa5fd1943c7b386f172d6893dbfba10b&' \
                'fltt=2&invt=2&' \
                'fields=f55,f84,f85,f92,f173,f105,f116,f117,f186,f187,f188,f190,f162,f167,f189&' \
                'secid=1.{0}&' \
                'cb=jQuery1124012710863660614313_1641728965040&' \
                '_=1641728965057'

        for stockInfo in self.stock_list:
            stock_code = getattr(stockInfo, 'stock_code')
            market = getattr(stockInfo, 'market')
            if market == 'sh':
                stock_url = url_2.format(stock_code)
            elif market == 'sz':
                stock_url = url_1.format(stock_code)
            if stock_url:
                meta = {'name': getattr(stockInfo, "stock_name"), 'code': stock_code}
                yield Request(stock_url, callback=self.parse_concept_item, meta=meta)

    def __init__(self, stock_list):
        self.stock_list = stock_list
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse_concept_item(self, response):
        current_app.logger.info('{}', response)
        '''
        股票概念信息，筹码集中度+资金数据+财报数据
        :param response:
        :return:
        '''

        stock_code = response.meta["code"]  ##取出数据
        stock_name = response.meta["name"]  ##取出数据

        item = StockFundamentalItem()
        item['stock_name'] = stock_name
        item['stock_code'] = stock_code

        text = response.text
        startIndex = text.find('(')
        endIndex = text.rfind(')')
        json_str = text[startIndex + 1:endIndex]
        dic_res = json.loads(json_str)
        data_ = dic_res['data']
        item['per_earnings'] = data_['f55']  # 每股收益ֵ'
        item['net_assets'] = data_['f92']  # 净资产
        item['net_profit'] = data_['f105']  # 净利润(亿)
        item['PE'] = data_['f162']  # 动态市盈率
        item['PB'] = data_['f167']  # 市净率
        item['gross_margin'] = data_['f186']  # 毛利率
        item['net_interest_rate'] = data_['f187']  # 净利率
        item['ROE'] = data_['f173']
        item['debt_ratio'] = data_['f188']  # 负债率
        item['UDPPS'] = data_['f190']  # '每股未分配利润(元)'
        item['market_time'] = data_['f189']  # 上市时间
        item['create_time'] = datetime.now()

        yield item
